package de.nordakademie.hausarbeit.configwriter.validation;

import org.eclipse.xtext.validation.Check;

import de.nordakademie.hausarbeit.configwriter.strutsConfDsl.Action;
import de.nordakademie.hausarbeit.configwriter.strutsConfDsl.Method;
import de.nordakademie.hausarbeit.configwriter.strutsConfDsl.PackageContainer;
import de.nordakademie.hausarbeit.configwriter.strutsConfDsl.StrutsConfDslPackage;
import de.nordakademie.hausarbeit.configwriter.strutsConfDsl.StrutsConfig;

public class StrutsConfDslJavaValidator extends AbstractStrutsConfDslJavaValidator {

	/* =======================================
	 * Some validation rules for the strutsConfDsl
	 *========================================*/
	/*
	 * validate that an action starts with a lower case
	 */
	@Check
	public void checkActionNameStartsWithLowerCase(Action action) {
		if (!Character.isUpperCase(action.getName().charAt(0))) {
			error("Action name should start with a capital",  StrutsConfDslPackage.Literals.ACTION__NAME);
		}
	}
	
	/*
	 * validate that an method name starts with a lower case
	 */
	@Check
	public void checkMethodNameStartsWithLowerCase(Method method) {
		if (!Character.isLowerCase(method.getName().charAt(0))) {
			error("Method name should start with a lower case",  StrutsConfDslPackage.Literals.METHOD__NAME);
		}
	}
	
	/*
	 * validate that only letters are in the method name
	 */
	@Check
	public void checkMethodNameOnlyIncludesCharacters(Method method) {
		boolean tmp = true;
		for(Character c : method.getName().toCharArray()) {
			if(!Character.isLetter(c)) {
				tmp = false;
				break;
			}
		}
		if(!tmp) {
			error("Method name should only include letters",  StrutsConfDslPackage.Literals.METHOD__NAME);
		}
	}
	
	/*
	 * validate that only letters are in the action name
	 */
	@Check
	public void checkActionNameOnlyIncludesCharacters(Action action) {
		boolean tmp = true;
		for(Character c : action.getName().toCharArray()) {
			if(!Character.isLetter(c)) {
				tmp = false;
				break;
			}
		}
		if(!tmp) {
			error("Action name should only include letters",  StrutsConfDslPackage.Literals.ACTION__NAME);
		}
	}
	
	/*
	 * validate that the action name is not empty
	 */
	@Check
	public void checkActionNameIsNotEmtpy(Action action) {
		if(action.getName().isEmpty()) {
			error("Name of action cannot be empty!",StrutsConfDslPackage.Literals.ACTION__NAME);
		}
	}
	
	/*
	 * validate that the method name is not empty
	 */
	@Check
	public void checkMethodNameIsNotEmtpy(Method method) {
		if(method.getName().isEmpty()) {
			error("Name of method cannot be empty!",StrutsConfDslPackage.Literals.METHOD__NAME);
		}
	}
	
	/*
	 * validate that the package container name is not empty
	 */
	@Check
	public void checkPackageContainerNameIsNotEmtpy(PackageContainer pc) {
		if(pc.getName().isEmpty()) {
			error("Name of PackageContainer cannot be empty!",StrutsConfDslPackage.Literals.PACKAGE_CONTAINER__NAME);
		}
	}
	
	/*
	 * validate that a struts config has at least one action
	 */
	@Check
	public void checkStrutsConfigHasAtLeastOneAction(StrutsConfig config) {
		if(config.getPackage()!=null) {
			PackageContainer p = config.getPackage();
			if(p.getActions().isEmpty()){
				error("Struts config must have at least one action", StrutsConfDslPackage.Literals.PACKAGE_CONTAINER__ACTIONS);
			}
		}
	}
}
